package leetcode.code1562;

import java.util.Map;
import java.util.TreeMap;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int findLatestStep(int[] arr, int m) {
		if (arr.length == m) {
			return m;
		}
		TreeMap<Integer, Integer> tm = new TreeMap<>();
		tm.put(0, arr.length + 1);
		for (int i = arr.length - 1; i >= 0; i--) {
			int num = arr[i];
			Map.Entry<Integer, Integer> entry = tm.floorEntry(num);
			int l = entry.getKey(), r = entry.getValue();
			if (num - l - 1 == m) {
				return i;
			}
			if (r - num - 1 == m) {
				return i;
			}
			tm.put(l, num);
			tm.put(num, r);
		}
		return -1;
	}

	@Override
	public void debug4() {
		H.compare(2, this.findLatestStep(H.str2array3("[2,1]"), 2));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
